package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.vo.TSystemPrivategroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUserprivategroup;

public class SystemPrivateGroupBO implements ISystemPrivateGroupBO {
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	
	
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	public Integer addSystemPrivateGroup(
			TSystemPrivategroup systemPrivategroup,
			List<TSystemUserprivategroup> systemUserprivategroupList) {
		// TODO Auto-generated method stub
		
		Integer groupId =  hibernateDao.save(systemPrivategroup);
		for (TSystemUserprivategroup systemUserallgroup : systemUserprivategroupList) {
			systemPrivategroup.setId(groupId);
			systemUserallgroup.setSystemPrivategroup(systemPrivategroup);
			hibernateDao.saveObject(systemUserallgroup);
		}
		return groupId;
	}

	public Boolean addTSystemUserPrivategroupById(
			TSystemUserprivategroup systemUserprivategroup) {
		// TODO Auto-generated method stub
		hibernateDao.saveObject(systemUserprivategroup);
		return true;
	}

	public Boolean deleteSystemPrivateGroup(
			TSystemPrivategroup systemPrivategroup) {
		// TODO Auto-generated method stub
		try {
			systemPrivategroup.setState(0);
			hibernateDao.update(systemPrivategroup);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public Boolean deleteTSystemUserPrivategroupById(Integer id) {
		// TODO Auto-generated method stub
		List<TSystemUserprivategroup> list = selectTSystemUserPrivategroupById(id);
		for (TSystemUserprivategroup systemUserallgroup : list) {
			hibernateDao.delete(systemUserallgroup);
		}
		
		return true;
	}

	public Boolean isSameSystemPrivateGroupName(Integer id, String postname) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPrivategroup.class);		
		criteria.add(Restrictions.eq("groupName", postname));
		criteria.add(Restrictions.not(Restrictions.eq("id", id)) );
		criteria.add(Restrictions.eq("state", 0));
		if (qbcDao.getCriteria(criteria).size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<TSystemPrivategroup> selectPrivateGroup(Integer first,
			Integer max) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemPrivategroup.class);
		List<Order> orderlist= new ArrayList<Order>();
		orderlist.add(Order.asc("orderNo"));
		return qbcDao.findPageByCriteria(criteria, orderlist, max, first).getItems();
	}

	public TSystemPrivategroup selectTSystemPrivategroupById(Integer id) {
		// TODO Auto-generated method stub
		TSystemPrivategroup systemAllgroup =  (TSystemPrivategroup) hibernateDao.findById(TSystemPrivategroup.class, id);
		return systemAllgroup;
	}

	public List<TSystemUserprivategroup> selectTSystemUserPrivategroupById(
			Integer id) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserprivategroup.class);
		criteria.add(Restrictions.eq("systemPrivategroup.id", id));
		List<TSystemUserprivategroup> systemUserallgroupList = qbcDao.getCriteria(criteria);
		return systemUserallgroupList;
	}

	public Integer selectTotalLine() {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TSystemUserprivategroup.class);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}

	public Boolean updateSystemPrivateGroup(
			TSystemPrivategroup systemPrivategroup) {
		// TODO Auto-generated method stub
		try {			
			hibernateDao.update(systemPrivategroup);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
